package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.PasssqDaoljc;
import com.zr.model.Course;
import com.zr.util.JDBCUtil;

public class PasssqDaoImplljc implements PasssqDaoljc{
	//往日志表中新增管理员处理申请日志 
	@Override
	public boolean insertBlog(int sid, String tname, String cname,String score,String zt, String sj, String newsj, String nr,Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" insert into blog (sid,tname,cname,score,zt,sqsj,sj,nr)");
		sql.append(" values(?,?,?,?,?,?,?,?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sid);
			pst.setString(2, tname);
			pst.setString(3, cname);
			pst.setString(4, score);
			pst.setString(5, zt);
			pst.setString(6, sj);
			pst.setString(7, newsj);
			pst.setString(8, nr);
			int i  = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	//添加成绩
	@Override
	public boolean insertScore(int sid, int cid, String score,Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" update s_c set ");
		sql.append(" score=? where sid=? and cid=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, score);
			pst.setInt(2, sid);
			pst.setInt(3, cid);
			int i  = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	//删除申请表中的申请数据
	@Override
	public boolean deleteSQ(int id,Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer("");
		sql.append(" DELETE FROM applyscore");
		sql.append(" WHERE id=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, id);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	//通过课程名找到课程id
	@Override
	public int findCidByCname(String cname) {
		int i = 0;
		StringBuffer sql = new StringBuffer("");
		sql.append(" select cid from course");
		sql.append(" WHERE cname=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, cname);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				i = rs.getInt("cid");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	//向教师申请日志表中添加数据
	@Override
	public boolean insertTblog(int sid,String tname, String sname, String cname, String score, String zt, String sqsj, String sj,
			String nr, Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" insert into teablog (sid,tname,sname,cname,score,zt,sqsj,sj,nr)");
		sql.append(" values(?,?,?,?,?,?,?,?,?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sid);
			pst.setString(2, tname);
			pst.setString(3, sname);
			pst.setString(4, cname);
			pst.setString(5, score);
			pst.setString(6, zt);
			pst.setString(7, sqsj);
			pst.setString(8, sj);
			pst.setString(9, nr);
			int i  = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	//通过sid查找学生姓名
	@Override
	public String findStuname(int sid) {
		String s = "";
		StringBuffer sql = new StringBuffer("");
		sql.append(" select sname from student");
		sql.append(" WHERE sid=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				s = rs.getString("sname");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return s;
	}

}
